<?php
/* SVN FILE: $Id: item_uploadavatar.php 247 2009-10-22 15:31:39Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Upload an Avatar item
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 247 $
* @lastrevision $Date: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/items/own/item_uploadavatar.php $
*/

class item_uploadavatar extends templateItem
{
	/**
	 * The default name of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultName = "Upload Your Avatar";
	
	/**
	 * The default description of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultDescription = "Upload an avatar for you to use. This item will bypass the group setting on uploading avatars.";
	
	// Inherited Properties
	/**
	 * Item setting to allow multiple instances within ibMarket 
	 *
	 * @access	protected
	 * @var		boolean
	 */
	protected $allowMultiple = true;
	
	/**
	 * Additional settings when adding/editing the item
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminForm()
	{
		return;
	}
	
	/**
	 * Saving to adding/editing the item
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	array	[$input]		Relavent input relating to the item
	 * @return	void
	 */
	public function adminSave(array &$item_info, array $input)
	{
		return;
	}
	
	/**
	 * When quick saving the price and stock
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	string	$type			Possible values: 'stockprice', more possibly in the future
	 * @return	void
	 */
	public function adminQuickSave(array &$item_info, $type)
	{
		return;
	}
	
	/**
	 * When removing the item from ibMarket
	 *
	 * @access	public
	 * @return	void
	 */
	public function adminRemove()
	{
		return;
	}
	
	/**
	 * Viewing the file edits if any
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminViewFileEdits()
	{
		return false;
	}
	
	/**
	 * Any admin AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function adminAjax()
	{
		return;
	}
	
	/**
	 * Check and modify the intentory item information before storing in the database
	 *
	 * @access	public
	 * @param	array	&$invItem		The new inventory item information to be stored in the database.
	 * @return	bool	true if the item is successfully created, false otherwise
	 */
	public function publicOnCreateItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When using the item
	 * Show a form and validate the form
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	string|bool				string if there is a form to be used, true if successfully used, false otherwise
	 */
	public function publicUseItem(array &$invItem)
	{
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_items' ) );
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_usercp' ), 'core' );
		
		if($this->request['request_method'] == 'post')
		{
			try
			{
				IPSMember::getFunction()->saveNewAvatar($this->memberData['member_id'],'upload_avatar','','','','');
			}
			catch( Exception $error )
			{
				$msg = $error->getMessage();
				
				switch( $msg )
				{
					case 'NO_MEMBER_ID':
						$this->registry->getClass('output')->showError('saveavatar_nomem');
					break;
					case 'NO_PERMISSION':
						$this->registry->getClass('output')->showError('saveavatar_noperm');
					break;
					case 'UPLOAD_NO_IMAGE':
						$this->registry->getClass('output')->showError('saveavatar_nofile');
					break;
					case 'UPLOAD_INVALID_FILE_EXT':
						$this->registry->getClass('output')->showError('saveavatar_noimg');
					break;
					case 'UPLOAD_TOO_LARGE':
						$this->registry->getClass('output')->showError('saveavatar_toobig');
					break;
					case 'UPLOAD_CANT_BE_MOVED':
						$this->registry->getClass('output')->showError('saveavatar_chmod');
					break;
					case 'UPLOAD_NOT_IMAGE':
						$this->registry->getClass('output')->showError('saveavatar_badimg');
					break;
					case 'NO_AVATAR_TO_SAVE':
						$this->registry->getClass('output')->showError('saveavatar_noimg2');
					break;
					case 'INVALID_FILE_EXT':
						$this->registry->getClass('output')->showError('saveavatar_badimgext');
					break;
				}
			}
			
			$this->deleteItems($invItem);
			
			$this->writeLog($invItem, "Uploaded avatar");
			
			$this->registry->getClass('output')->redirectScreen( $this->lang->words['avatar_updated'], $this->settings['base_url_with_app'] . "module=inventory" );
		}
		
		$html = <<<EOF
<form id='uploadAvatarForm' enctype="multipart/form-data" method="post" action="{$this->settings['base_url_with_app']}module=inventory&amp;section=item&amp;item={$invItem['i_id']}">
	<div id='uploadAvatar'class='block_wrap'>
		<h3 id='uploadAvatarHeader' class='maintitle'>{$this->item['item_name']}</h3>
		<fieldset class='row1'>
			<h3>{$this->lang->words['avatar_url_title']}</h3>
			<p class='message unspecific'>
EOF;
		list( $this->settings['maxWidth'], $this->settings['maxHeight'] ) = explode( "x", strtolower($this->settings['avatar_dims'] ) );
		$html .= sprintf( $this->lang->words['av_text_url'], $this->settings['maxWidth'], $this->settings['maxHeight'] );
		$html .= <<<EOF
					<br />

EOF;
		$html .= sprintf( $this->lang->words['av_text_upload'], $this->settings['avup_size_max'] );
		$this->lang->words['av_allowed_files'] = sprintf($this->lang->words['av_allowed_files'], implode (' .', explode( "|", $this->settings['avatar_ext'] ) ) );
		$html .= <<<EOF
				<br />{$this->lang->words['av_allowed_files']}
			</p>
			<ul>
				<li class='field'>
					<label for='upload_avatar'>{$this->lang->words['av_upload']}</label>
					<input type="file" size="30" name="upload_avatar" id="upload_avatar" />
				</li>
			</ul>

EOF;
		if( ! $this->settings['disable_ipbsize'] )
		{
			$html .= <<<EOF
			<p><strong>{$this->lang->words['av_autosize']}</strong><br />({$this->lang->words['av_as_desc']})</p>

EOF;
		}
		else
		{
			$html .= <<<EOF
			<ul>
				<li class='field'>
					{$this->lang->words['width']}
					<input type="text" size="3" name="man_width" />
				</li>
				<li class='field'>
					{$this->lang->words['height']}
					<input type="text" size="3" name="man_height" />
				</li>
			</ul>

EOF;
		}
		$html .= <<<EOF
		</fieldset>
		<fieldset class="submit">
			<input class="input_submit" type="submit" value="{$this->lang->words['upload_avatar']}" name="submit" />
		</fieldset>
	</div>
</form>
EOF;
		return $html;
	}
	
	/**
	 * Any AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function publicAjax(array &$invItem)
	{
		return;
	}
	
	/**
	 * When the item is being sold back
	 *
	 * @access	public
	 * @param	array	&$invItem	The inventory item information
	 * @return	bool				true if successful, false otherwise
	 */
	public function publicOnResoldItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When the item is being restocked
	 *
	 * @access	public
	 * @param	array	&$item	The inventory item information
	 * @return	void
	 */
	public function onItemRestock(array &$item)
	{
		return;
	}
}
